<?php

namespace addons\Summary\common\models;

use Yii;

/**
 * This is the model class for table "{{%addon_oa_study}}".
 *
 * @property int $id 编号
 * @property int $cate_id 分类
 * @property string $title 标题
 * @property string $content 内容
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Summary extends \common\models\base\BaseModel
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_summary}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'cate_id','content'], 'required'],
            [['merchant_id','member_id','cate_id', 'created_at', 'updated_at', 'status'], 'integer'],
            [['title'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '用户',
            'title' => '标题',
            'cate_id' => '类型',
            'content' => '内容',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * 关联用户
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(\common\models\merchant\Member::class, ['id' => 'member_id']);
    }
    public function getCate()
    {
        return $this->hasOne(SummaryCate::class, ['id' => 'cate_id']);
    }
    public function beforeSave($insert)
    {
        if ($insert) {
            if(Yii::$app->id == 'backend'||Yii::$app->id == 'merchant')
                $this->member_id = Yii::$app->user->id;
            else
                $this->member_id = Yii::$app->user->identity->member_id;
            $this->merchant_id = Yii::$app->user->identity->merchant_id;

        }
        return parent::beforeSave($insert);
    }
}
